<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;

/**
 * 产品介绍
 *
 * @icon fa fa-circle-o
 */
class Productinfo extends Backend
{
    
    /**
     * Productinfo模型对象
     * @var \app\admin\model\product\Productinfo
     */
    protected $model = null;
    protected $modelValidate = true;
    // 不限制访问
    protected $noNeedLogin = ['detail'];
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\product\Productinfo;

    }
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    
                    ->where($where)
                    ->order('sort desc')
                    ->count();

            $list = $this->model
                    
                    ->where($where)
                    ->order('sort desc')
                    ->limit($offset, $limit)
                    ->select();

            // foreach ($list as $row) {
            //     $row->visible(['id','product_name','text','createtime','updatetime']);
                
            // }
            $list = $list->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("row", $row);
        $this->view->engine->layout(false);
        return $this->view->fetch();
    }
    // 上传图片
    public function uploadImg()
    {
        $result = $this->upImg();
        return json(['a' => $result]);
    }
    public function randName()
    {
        return md5(rand(1000, 9999) . rand(1000, 9999) . time());
    }
    public function upImg()
    {
        $files = $this->request->file('file');
        /*截取后缀*/
        $suffix = substr($files->getInfo()['name'], strrpos($files->getInfo()['name'], '.'));
        $arr = array('.jpg', '.png', '.jpeg', '.gif');
        /*判断是否为图片*/
        if (in_array($suffix, $arr)) {
            //生成随机名
            $str = $this->randName() . $suffix;
            //上传文件到img文件
            $path = date('Ymd'); // 接收文件目录
            //判断有没有这个文件
            if (!file_exists('uploads/images/' . $path)) {
                mkdir('uploads/images/' . $path, 0777, true);
            }

            $filename = 'uploads/images/' . $path . '/' . $str;
            //临时文件移动到文件夹内
            move_uploaded_file($files->getInfo()['tmp_name'], $filename);
            return $this->request->domain() . '/' . $filename;
        } else {
            return false;
        }
    }
}
